package com.xfg.search.controller;

import com.xfg.search.entity.ChineseDrug;
import com.xfg.search.entity.Composition;
import com.xfg.search.entity.DrugPromoted;
import com.xfg.search.entity.Oil;
import com.xfg.search.service.AdminService;
import com.xfg.search.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    //    全文检索
    @GetMapping("search")
    public Map<String,Object> search(Integer id, String word) throws IOException {
        log.info(id+word);
        HashMap<String,Object> map=new HashMap<>();
        if (id==1){
            List<Oil>oilList=userService.searchOil(word);
            List<Oil> oils= userService.searchByOil(word);
            map.put("oils", oils);
            map.put("oilList",oilList);
        }else if(id==2){
            List<ChineseDrug> drugs= userService.searchByChineseDrug(word);
            List<ChineseDrug> drugList= userService.findDrug(word);
            map.put("drugs", drugs);
            map.put("drugList", drugList);
        }else if(id==3){
            List<Composition> compositions= userService.searchByComposition(word);

            map.put("compositions", compositions);
            System.out.println(compositions);
        }else if(id==5){
            List<Oil> oils= userService.searchByEfficacy(word);
            map.put("oils", oils);
            System.out.println(oils);
        }else if(id==7){
            List<Oil> oils= userService.findCompositionByOil(word);
            map.put("oils", oils);
            System.out.println(oils);
        }else if(id==8){
            List<Oil> oils= userService.findDrugPromotedByOil(word);
            map.put("oils", oils);
            System.out.println(oils);
        }else if(id==9){
            List<String>oilName=userService.findOilByDrug(word);
            map.put("oilName",oilName);
            System.out.println(oilName);
        }else if(id==10){
            Oil oil=userService.findOil(word);
            map.put("oil",oil);
        }else if (id==11){
            List<String> result = new ArrayList<>();
            List<String>oilList=userService.findOilByDrugPromoted(word);
            for (String s:oilList){
                result.add(s.replace("挥发油","油"));
            }

            map.put("oilName",result);
        }
        else {
            List<DrugPromoted> drugPromoteds= userService.searchByDrugPromoted(word);

            map.put("drugPromoteds", drugPromoteds);
            System.out.println(drugPromoteds);
        }


        return map;
    }

    @GetMapping("searchName")
    public Map<String,Object> searchByName(Integer id, String word) throws IOException {
        log.info(id+word);
        HashMap<String,Object> map=new HashMap<>();
        if (id==1){

            List<Oil>oilList=userService.searchOil(word);
            List<Oil> oils= userService.searchByOil(word);
            map.put("oils", oils);
            map.put("oilList",oilList);
        }else if(id==2){
            List<ChineseDrug> drugs= userService.searchByChineseDrug(word);
            map.put("drugs", drugs);

        }else if(id==3){
            List<Composition> compositions= userService.searchByComposition(word);
            map.put("compositions", compositions);
            System.out.println(compositions);
        }else if(id==5){
            List<Oil> oils= userService.searchByEfficacy(word);
            map.put("oils", oils);
            System.out.println(oils);
        }
        else {
            List<DrugPromoted> drugPromoteds= userService.searchByDrugPromoted(word);
            map.put("drugPromoteds", drugPromoteds);
            System.out.println(drugPromoteds);
        }


        return map;
    }
}
